package com.example.fill;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.PointF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;

public class BserView extends View {
    public BserView(Context context) {
        super(context);
    }

    public BserView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public BserView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    private Paint paint = new Paint();
    private Path path = new Path();
    private Point startPoint = new Point(200,200);
    private Point middlePoint = new Point(400,450);
    private Point endPoint = new Point(600,250);
    private PointF leftControl = new PointF();
    private PointF rightControl = new PointF();

    private final float rate = 0.25f;


    @Override
    protected void onDraw(Canvas canvas) {
        paint.setColor(Color.BLACK);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(5f);
        int k = (endPoint.y - startPoint.y) / (endPoint.x - startPoint.x);
        int b = middlePoint.y - k * middlePoint.x;
        float leftX = middlePoint.x - (middlePoint.x - startPoint.x) * rate;
        float leftY = k * leftX + b;
        leftControl.set(leftX,leftY);
        float rightX = middlePoint.x + (endPoint.x - middlePoint.x) * rate;
        float rightY = k * rightX + b;
        rightControl.set(rightX,rightY);

        path.cubicTo(leftControl.x,leftControl.y,rightControl.x,rightControl.y,endPoint.x,endPoint.y);
        canvas.drawPath(path,paint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        super.performClick();
        return super.onTouchEvent(event);
    }

    @Override
    public boolean performClick() {
        return super.performClick();
    }
}
